package com.service.impl;

import com.mapper.CollectionsMapper;
import com.pojo.Collections;
import com.pojo.Result;
import com.service.CollectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CollectionsServiceImpl implements CollectionsService {

    @Autowired
    private CollectionsMapper collectionsMapper;

    @Override
    public Result<Void> collectNote(Integer userId, Integer noteId) {
        try {
            // 检查是否已收藏
            if (collectionsMapper.exists(userId, noteId) > 0) {
                return Result.error("您已经收藏过该笔记");
            }

            // 创建收藏记录
            Collections collection = new Collections();
            collection.setUserId(userId);
            collection.setNoteId(noteId);
            collection.setCreatedAt(LocalDateTime.now());

            int result = collectionsMapper.insert(collection);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("收藏笔记失败");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> uncollectNote(Integer userId, Integer noteId) {
        try {
            int result = collectionsMapper.delete(userId, noteId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("取消收藏失败，可能未收藏");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> uncollectAllByUserId(Integer userId) {
        try {
            int result = collectionsMapper.deleteByUserId(userId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有收藏也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> uncollectAllByNoteId(Integer noteId) {
        try {
            int result = collectionsMapper.deleteByNoteId(noteId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有被收藏也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Boolean> isCollected(Integer userId, Integer noteId) {
        try {
            int exists = collectionsMapper.exists(userId, noteId);
            return Result.success(exists > 0);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Integer> getCollectionCountByNoteId(Integer noteId) {
        try {
            int count = collectionsMapper.countByNoteId(noteId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Integer> getCollectionCountByUserId(Integer userId) {
        try {
            int count = collectionsMapper.countByUserId(userId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Collections>> getCollectionsByUserId(Integer userId) {
        try {
            List<Collections> collections = collectionsMapper.selectByUserId(userId);
            return Result.success(collections);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Collections>> getCollectionsByNoteId(Integer noteId) {
        try {
            List<Collections> collections = collectionsMapper.selectByNoteId(noteId);
            return Result.success(collections);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }
}